package com.snda.lib.util;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import com.snda.lib.proxy.FileProxy;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class ImageHelper {

	/**
	 * @param 同步获取图片
	 *            图片不会在本地进行缓存
	 * @return
	 */
	public static Bitmap fetchImage(String strUrl, String strLocalPath) {
		try {

			FileProxy proxy = new FileProxy(strUrl, strLocalPath);
			InputStream is = proxy.getInputStream();
			if (is == null) {
				return null;
			}

			BufferedInputStream bIs = new BufferedInputStream(is);
			bIs.mark(0);
			return BitmapFactory.decodeStream(is);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * @desc 从指定的路径图片生成Drawable
	 * @param strPath
	 * @return
	 */
	public static Drawable getResourceDrawable(String strPath) {
		return Drawable.createFromPath(strPath);
	}

	/**
	 * @desc 从指定的路径生成位图对象
	 * @param strPath
	 * @return
	 */
	public static Bitmap getResourceBitmap(String strPath) {
		Drawable dw = getResourceDrawable(strPath);
		return drawable2Bitmap(dw);
	}

	/**
	 * @desc 从assets中读取出drawable对象
	 * @param ctx
	 * @param strPath
	 * @return
	 */
	public static Drawable getDrawableFromAssets(Context ctx, String strPath) {
		AssetManager am = null;
		am = ctx.getAssets();

		try {
			InputStream is = am.open(strPath);
			Drawable dw = Drawable.createFromStream(is, "");
			am.close();
			return dw;
		} catch (IOException e) {
			return null;
		}
	}

	/**
	 * 位图转换成Drawable
	 * 
	 * @param bm
	 * @return
	 */
	public static Drawable bitmap2Drawable(Bitmap bm) {
		Drawable drawable = new BitmapDrawable(bm);
		return drawable;
	}

	/**
	 * @desc Drawable转换成Bitmap
	 * @param drawable
	 * @return
	 */
	public static Bitmap drawable2Bitmap(Drawable drawable) {
		if (drawable == null)
			return null;

		Bitmap bitmap = Bitmap
				.createBitmap(
						drawable.getIntrinsicWidth(),
						drawable.getIntrinsicHeight(),
						drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
								: Bitmap.Config.RGB_565);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
				drawable.getIntrinsicHeight());
		drawable.draw(canvas);
		return bitmap;
	}

	/**
	 * @desc 将位图转换成字节
	 * @param bitmap
	 * @return
	 */
	public static byte[] bitmap2Bytes(Bitmap bitmap) {
		if (bitmap == null) {
			return null;
		}

		// 将Bitmap压缩成PNG编码，质量为100%存储
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);// 除了PNG还有很多常见格式，如jpeg等。
		return os.toByteArray();
	}

	/**
	 * @desc 将给定的字节专程bitmap
	 * @param bytes
	 * @return
	 */
	public static Bitmap bytes2Bitmap(byte[] bytes) {

		Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
		return bmp;
	}

	/**
	 * @desc 自适应的读取给定路径中的位图
	 * @param strImagePath
	 * @return
	 */
	public static Bitmap fittableDecodeFromPath(String strImagePath) {
		File imageFile = new File(strImagePath);
		double nFileSize = imageFile.length();

		// 超过2M
		if (nFileSize >= 1024 * 2048) {
			BitmapFactory.Options options = new BitmapFactory.Options();
			options.inSampleSize = 8;
			return BitmapFactory.decodeFile(strImagePath, options);
		}

		// 超过1M
		else if (nFileSize >= 1024 * 1024) {
			BitmapFactory.Options options = new BitmapFactory.Options();
			options.inSampleSize = 6;
			return BitmapFactory.decodeFile(strImagePath, options);
		}

		// 超过512k将压缩处理
		else if (nFileSize >= 1024 * 512) {

			BitmapFactory.Options options = new BitmapFactory.Options();
			options.inSampleSize = 4;
			return BitmapFactory.decodeFile(strImagePath, options);
		} else {

			return BitmapFactory.decodeFile(strImagePath);
		}
	}
}
